package com.caipin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.caipin.dao.CourseCurrentDao;
import com.caipin.dao.CourseSubjectsDao;
import com.caipin.dao.CourseTableDao;
import com.caipin.dao.CourseTimetableDao;
import com.caipin.domain.table.CourseCurrent;
import com.caipin.domain.table.CourseSubjects;
import com.caipin.domain.table.CourseTable;
import com.caipin.domain.table.CourseTimetable;
import com.caipin.domain.web.PageBean;
import com.caipin.service.CourseManagementService;
import com.caipin.service.CourseSubjectsService;
import com.caipin.service.CourseTableService;
import com.caipin.service.CourseTimetableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: zhunian0322@163.com
 * @DateTime: 2020-07-30 15:01
 * @Description: TODO
 */
@Service
public class CourseManagementServiceImpl implements CourseManagementService {

    @Autowired
    private CourseTimetableDao courseTimetableDao;

    @Autowired
    private CourseTimetableService courseTimetableIService;

    @Autowired
    private CourseSubjectsDao courseSubjectsDao;

    @Autowired
    private CourseSubjectsService courseSubjectsIService;

    @Autowired
    private CourseTableDao courseTableDao;

    @Autowired
    private CourseTableService courseTableIService;

    @Autowired
    private CourseCurrentDao courseCurrentDao;

    public int importTimeTableToMysql(List<CourseTimetable> timetableList) {
        this.courseTimetableDao.truncateData();
        boolean b = this.courseTimetableIService.saveBatch(timetableList);
        if (!b)
            return 0;
        return timetableList.size();
    }

    public List<CourseTimetable> findCourseTimetableList() {
        return this.courseTimetableDao.selectList(null);
    }

    public CourseTimetable findCourseTimetableById(Long id) {
        return (CourseTimetable)this.courseTimetableDao.selectById(id);
    }

    public void addCourseTimetable(CourseTimetable courseTimetable) {
        this.courseTimetableDao.insert(courseTimetable);
    }

    public void updateCourseTimetable(CourseTimetable courseTimetable) {
        this.courseTimetableDao.updateById(courseTimetable);
    }

    public int importCourseSubjectsToMysql(List<CourseSubjects> courseSubjectsList) {
        this.courseSubjectsDao.truncateData();
        boolean b = this.courseSubjectsIService.saveBatch(courseSubjectsList);
        if (!b)
            return 0;
        return courseSubjectsList.size();
    }

    public PageBean<CourseSubjects> findCourseSubjectsList(Integer pageNum, Integer pageSize) {
        Page<CourseSubjects> courseSubjectsPage = new Page(pageNum.intValue(), pageSize.intValue());
        IPage<CourseSubjects> page = this.courseSubjectsDao.selectPage((IPage)courseSubjectsPage, null);
        PageBean<CourseSubjects> pageBean = new PageBean();
        pageBean.setCurrPage(Long.valueOf(page.getCurrent()));
        pageBean.setPageSize(Integer.valueOf(page.getRecords().size()));
        pageBean.setTotalPage(Long.valueOf(page.getPages()));
        pageBean.setTotalCount(Long.valueOf(page.getTotal()));
        pageBean.setList(page.getRecords());
        return pageBean;
    }

    public PageBean<CourseSubjects> findCourseSubjectsList(Integer pageNum, Integer pageSize, String schoolYear, Integer semester, String courseName, String courseType, String grade, String classroom) {
        Page<CourseSubjects> courseSubjectsPage = new Page(pageNum.intValue(), pageSize.intValue());
        QueryWrapper<CourseSubjects> queryWrapper = new QueryWrapper();
        if (schoolYear != null)
            queryWrapper.eq("school_year", schoolYear);
        if (semester != null)
            queryWrapper.eq("semester", semester);
        if (courseName != null)
            queryWrapper.eq("name", courseName);
        if (courseType != null)
            queryWrapper.eq("type", courseType);
        if (grade != null)
            queryWrapper.eq("grade", grade);
        if (classroom != null)
            queryWrapper.eq("classroom", classroom);
        IPage<CourseSubjects> page = this.courseSubjectsDao.selectPage((IPage)courseSubjectsPage, (Wrapper)queryWrapper);
        PageBean<CourseSubjects> pageBean = new PageBean();
        pageBean.setCurrPage(Long.valueOf(page.getCurrent()));
        pageBean.setPageSize(Integer.valueOf(page.getRecords().size()));
        pageBean.setTotalPage(Long.valueOf(page.getPages()));
        pageBean.setTotalCount(Long.valueOf(page.getTotal()));
        pageBean.setList(page.getRecords());
        return pageBean;
    }

    public CourseSubjects findCourseSubjectsById(Long id) {
        return (CourseSubjects)this.courseSubjectsDao.selectById(id);
    }

    public void delCourseSubjectsById(Long id) {
        this.courseSubjectsDao.deleteById(id);
    }

    public void addCourseSubjects(CourseSubjects courseSubjects) {
        this.courseSubjectsDao.insert(courseSubjects);
    }

    public void updateCourseSubjects(CourseSubjects courseSubjects) {
        this.courseSubjectsDao.updateById(courseSubjects);
    }

    public int importCourseTableToMysql(List<CourseTable> courseTables) {
        this.courseTableDao.truncateData();
        boolean b = this.courseTableIService.saveBatch(courseTables);
        if (!b)
            return 0;
        return courseTables.size();
    }

    public PageBean<CourseTable> findCourseTableList(Integer pageNum, Integer pageSize) {
        Page<CourseTable> courseTablePage = new Page(pageNum.intValue(), pageSize.intValue());
        IPage<CourseTable> page = this.courseTableDao.selectPage((IPage)courseTablePage, null);
        PageBean<CourseTable> pageBean = new PageBean();
        pageBean.setCurrPage(Long.valueOf(page.getCurrent()));
        pageBean.setPageSize(Integer.valueOf(page.getRecords().size()));
        pageBean.setTotalPage(Long.valueOf(page.getPages()));
        pageBean.setTotalCount(Long.valueOf(page.getTotal()));
        pageBean.setList(page.getRecords());
        return pageBean;
    }

    public PageBean<CourseTable> findCourseTableList(Integer pageNum, Integer pageSize, String grade, String clbum, Integer week, Integer sections, String course_name) {
        Page<CourseTable> courseTablePage = new Page(pageNum.intValue(), pageSize.intValue());
        QueryWrapper<CourseTable> queryWrapper = new QueryWrapper();
        if (grade != null)
            queryWrapper.eq("grade", grade);
        if (clbum != null)
            queryWrapper.eq("clbum", clbum);
        if (week != null)
            queryWrapper.eq("week", week);
        if (sections != null)
            queryWrapper.eq("sections", sections);
        if (course_name != null)
            queryWrapper.eq("course_name", course_name);
        IPage<CourseTable> page = this.courseTableDao.selectPage((IPage)courseTablePage, (Wrapper)queryWrapper);
        PageBean<CourseTable> pageBean = new PageBean();
        pageBean.setCurrPage(Long.valueOf(page.getCurrent()));
        pageBean.setPageSize(Integer.valueOf(page.getRecords().size()));
        pageBean.setTotalPage(Long.valueOf(page.getPages()));
        pageBean.setTotalCount(Long.valueOf(page.getTotal()));
        pageBean.setList(page.getRecords());
        return pageBean;
    }

    public int delCourseTableList(List<Integer> ids) {
        return this.courseTableDao.deleteBatchIds(ids);
    }

    public void delCourseTable(Integer id) {
        this.courseTableDao.deleteById(id);
    }

    public CourseTable findCourseTableById(Long id) {
        CourseTable courseTable = (CourseTable)this.courseTableDao.selectById(id);
        return courseTable;
    }

    public void updateCourseTable(CourseTable courseTable) {
        this.courseTableDao.updateById(courseTable);
    }

    public void addCourseTable(CourseTable courseTable) {
        this.courseTableDao.insert(courseTable);
    }

    public List<CourseTable> findCourseTableListByWeek(int week) {
        QueryWrapper<CourseTable> queryWrapper = new QueryWrapper();
        queryWrapper.eq("week", Integer.valueOf(week));
        List<CourseTable> courseTableList = this.courseTableDao.selectList((Wrapper)queryWrapper);
        return courseTableList;
    }

    public CourseTimetable findCourseTimetableBySections(Integer sections) {
        CourseTimetable courseTimetable = (CourseTimetable)this.courseTimetableDao.selectOne((Wrapper)(new QueryWrapper()).eq("sections", sections));
        return courseTimetable;
    }

    public void addCourseCurrent(CourseCurrent courseCurrent) {
        this.courseCurrentDao.insert(courseCurrent);
    }

    public List<CourseTable> findCourseTableListByWeekAndSectionsAndGradeAndCourseName(Integer week, Integer sections, String grade, String courseName) {
        QueryWrapper<CourseTable> queryWrapper = new QueryWrapper();
        queryWrapper.eq("week", week);
        queryWrapper.eq("sections", sections);
        queryWrapper.eq("grade", grade);
        queryWrapper.eq("course_name", courseName);
        return this.courseTableDao.selectList((Wrapper)queryWrapper);
    }

    public int getCourseMaxSections() {
        return this.courseTimetableDao.getCourseMaxSections();
    }

}
